<template>
	<common-page title="密码到期重置">
		<el-steps :active="stepActive" simple finish-status="success">
		    <el-step title="填写新密码" />
		    <el-step title="完成重置" />
		</el-steps>
		<el-form v-if="stepActive==0" ref="form" :model="form" :rules="rules" :label-width="120">
			<el-form-item label="登录账号" prop="pAccountId">
				<el-input v-model="form.pAccountId" placeholder="请输入登录账号"></el-input>
				<div class="el-form-item-msg">请输入您的登录账号</div>
			</el-form-item>
			<el-form-item label="原密码" prop="pPassword">
				<el-input v-model="form.pPassword" show-password placeholder="原密码"></el-input>
				<div class="el-form-item-msg">请输入{{sysConfig.passWordStrength=='1'?'包含英文、数字':'任意字符'}}的{{sysConfig.passWordLength}}位以上密码</div>
			</el-form-item>
			<el-form-item label="新密码" prop="nPassword1">
				<el-input v-model="form.nPassword1" show-password placeholder="请输入新密码"></el-input>
				<div class="el-form-item-msg">请输入包{{sysConfig.passWordStrength=='1'?'包含英文、数字':'任意字符'}}的{{sysConfig.passWordLength}}位以上密码</div>
			</el-form-item>
			<el-form-item label="确认新密码" prop="nPassword2">
				<el-input v-model="form.nPassword2" show-password placeholder="请再一次输入新密码"></el-input>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="updateNewPassWord">提交</el-button>
			</el-form-item>
		</el-form>
		<el-result v-if="stepActive==1" icon="success" title="密码重置成功" sub-title="请牢记自己的新密码,返回登录后使用新密码登录">
			<template #extra>
				<el-button type="primary" @click="backLogin">返回登录</el-button>
			</template>
		</el-result>
	</common-page>
</template>

<script>
import {RSAEncrypt} from "@/utils/util"
	import commonPage from './components/commonPage'
	export default {
		components: {
			commonPage
		},
		data() {
			return {
				stepActive: 0,
				form: {
					pAccountId: "",
					pPassword: "",
					nPassword1: "",
					nPassword2: ""
				},
				sysConfig:{},
				rules: {
					pAccountId: [
						{ required: true, message: '请输入登录账号'}
					],
					pPassword: [
						{ required: true, message: '请输入原密码'}
					],
					nPassword1: [
						{ required: true, message: '请输入新的密码'}
					],
					nPassword2: [
						{ required: true, message: '请再次输入新的密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.nPassword1) {
								callback(new Error('两次输入密码不一致'));
							}else{
								callback();
							}
						}}
					],
				},
				disabled: false,
				time: 0
			}
		},
		mounted() {
			this.getSysConfigInitForReSetPassword();
		},
		methods: {
			updateNewPassWord(){
				this.$refs.form.validate(async (valid) => {
					if (valid) {
						let params =  {
								pAccountId: RSAEncrypt(this.form.pAccountId),
								pPassword:  RSAEncrypt(this.form.pPassword),
								nPassword1: RSAEncrypt(this.form.nPassword1),
								nPassword2: RSAEncrypt(this.form.nPassword2)
						};
						var res = await this.$API.system.user.updateNewPassWord.post(params);
						if(res.code == 200){
							this.$message.success(res.message);
							this.stepActive = 1
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			async getSysConfigInitForReSetPassword()
			{
				var res = await this.$API.system.sysConfigInit.getSysConfigInitForReSetPassword.get();
				if(res.code == 200){
					this.sysConfig = res.data;
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			backLogin(){
				this.$router.push({
					path: '/login'
				})
			}
		}
	}
</script>

<style scoped>


</style>
